package Sort;

import java.util.Arrays;

/**
 * @Author Fizz Pu
 * @Date 2021/4/1 下午3:04
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */
public class BubbleSort implements SortAlgrithom {
    @Override
    public void sort(int[] arr){
        if(arr == null){
            throw new IllegalArgumentException("null");
        }

        int length = arr.length;
        boolean completed = true;
        for(int i = 0; i < length - 1; i++){
            for(int j = 0; j <  length - i - 1; j++){
                if(arr[j] > arr[j + 1]){ // 稳定排序
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    completed = false;
                }
            }

            if(completed){
                break;
            }
        }

        System.out.println(Arrays.toString(arr));
    }
}
